package com.zzjx.common.http.Impl;

import com.redxun.common.tool.BeanUtil;
import com.zzjx.common.http.HttpRtnModel;
import com.zzjx.common.http.IHttp;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 基于Spring-ResfulTemplate
 *
 * @author zhaojiaxu
 * @date 2021/06/28 15:28
 */
@Component
public class ResfulTemplateImpl implements IHttp {
    private String defaultCharset = "utf-8";

    @Resource(name = "restTemplateExternal")
    RestTemplate restTemplate;

    private HttpHeaders getRequestHeaders(Map<String, String> reqHeaders) {
        HttpHeaders requestHeaders = new HttpHeaders();
        if (BeanUtil.isNotEmpty(reqHeaders)) {
            for (Map.Entry<String, String> ent : reqHeaders.entrySet()) {
                requestHeaders.add(ent.getKey(), ent.getValue());
            }
        }
        return requestHeaders;
    }

    private HttpRtnModel sendRequest(HttpEntity requestEntity, String url, HttpMethod method, String charset) throws Exception {
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity, String.class);
        HttpRtnModel rtnModel = new HttpRtnModel();
        rtnModel.setStatusCode(response.getStatusCodeValue());
        HttpHeaders respHeaders = response.getHeaders();
        for (Map.Entry<String, List<String>> ent : respHeaders.entrySet()) {
            rtnModel.addHeader(ent.getKey(), ent.getValue().get(0));
        }
        rtnModel.setContent(new String(response.getBody().getBytes(), charset));
        return rtnModel;
    }

    private HttpRtnModel postFromUrl(String url, Map<String, String> reqHeaders, Map<String, String> params, String charset) throws Exception {
        HttpHeaders headers = getRequestHeaders(reqHeaders);
        HttpEntity<Object> requestEntity = new HttpEntity<>(params, headers);
        return sendRequest(requestEntity, url, HttpMethod.GET, charset);
    }

    private HttpRtnModel postFromUrl(String url, Map<String, String> reqHeaders, String content, String charset) throws Exception {
        HttpHeaders headers = getRequestHeaders(reqHeaders);
        HttpEntity<String> requestEntity = new HttpEntity<>(content, headers);
        return sendRequest(requestEntity, url, HttpMethod.POST, charset);
    }

    private HttpRtnModel getFromUrl(String url, Map<String, String> reqHeaders, Map<String, String> params, String charset) throws Exception {
        HttpHeaders headers = getRequestHeaders(reqHeaders);
        HttpEntity<Object> requestEntity = new HttpEntity<>(params, headers);
        return sendRequest(requestEntity, url, HttpMethod.GET, charset);
    }

    private HttpRtnModel getFromUrl(String url, Map<String, String> reqHeaders, String content, String charset) throws Exception {
        HttpHeaders headers = getRequestHeaders(reqHeaders);
        HttpEntity<String> requestEntity = new HttpEntity<>(content, headers);
        return sendRequest(requestEntity, url, HttpMethod.GET, charset);
    }

    @Override
    public HttpRtnModel getFromUrl(String url, Map<String, String> reqHeaders, Map<String, String> params) throws Exception {
        HttpRtnModel rtnModel = getFromUrl(url, reqHeaders, params, defaultCharset);
        return rtnModel;
    }

    @Override
    public HttpRtnModel getFromUrl(String url, Map<String, String> reqHeaders, String content) throws Exception {
        HttpRtnModel rtnModel = getFromUrl(url, reqHeaders, content, defaultCharset);
        return rtnModel;
    }

    /**
     * 向服务端发送请求并接收返回的数据。
     *
     * @param url
     * @param reqHeaders cookie数据
     *                   JSESSIONID=eadc003dd;\"
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public HttpRtnModel postFromUrl(String url, Map<String, String> reqHeaders, Map<String, String> params) throws Exception {
        HttpRtnModel rtn = postFromUrl(url, reqHeaders, params, defaultCharset);
        return rtn;
    }

    /**
     * 发送请求内容为字符串。
     *
     * @param url
     * @param reqHeaders
     * @param content
     * @return
     * @throws Exception
     */
    @Override
    public HttpRtnModel postFromUrl(String url, Map<String, String> reqHeaders, String content) throws Exception {
        HttpRtnModel rtnModel = postFromUrl(url, reqHeaders, content, defaultCharset);
        return rtnModel;
    }
}
